package com.ssii.www.myproject.service.impl;

import com.ssii.www.myproject.query.BaseQuery;
import com.ssii.www.myproject.repository.BaseRepository;
import com.ssii.www.myproject.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 事务不涉及写操作就设置为只读，support 有就是用事务，没有就不用
 *
 * @param <T>
 * @param <ID>
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID> {
  @Autowired
  private BaseRepository<T, ID> baseRepository;

  @Transactional//默认为可读写 默认为REQUIRED 有就是用事务，没有就创建新的事务
  @Override
  public void save(T t) {
    baseRepository.save(t);
  }

  @Transactional
  @Override
  public void delete(ID id) {
    baseRepository.delete(id);
  }

  @Override
  public T findOne(ID id) {
    return baseRepository.findOne(id);
  }

  @Override
  public List<T> findAll() {
    return baseRepository.findAll();
  }

  @Override
  public Page<T> findPageByQuery(BaseQuery query) {
    return baseRepository.findPageByQuery(query);
  }

  @Override
  public List<T> findByQuery(BaseQuery baseQuery) {
    return baseRepository.findByQuery(baseQuery);
  }

  @Override
  public List<T> findByJpql(String jpql, Object... params) {
    return baseRepository.findByJpql(jpql, params);
  }
}
